package com.uenpay.util.xml;


import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

import java.io.StringWriter;
import java.util.Map;

/**
 * * 创建XML文件
 */
public class CreateXML {

	private static final String TAG = "createXML";

	/**
	 * 创建xml文件
	 * @param map xml内容集合
	 * @return xml字符串
	 */
	public static String create(Map<String, String> map) {
		try {
			XmlPullParserFactory pullFactory = XmlPullParserFactory.newInstance();
			XmlSerializer xmlSerializer = pullFactory.newSerializer();
			StringWriter xmlWriter = new StringWriter();
			xmlSerializer.setOutput(xmlWriter);
			xmlSerializer.startDocument("UTF-8", null);
			xmlSerializer.startTag("", "EPOSPROTOCOL");
			for (Map.Entry<String, String> entry : map.entrySet()) {
				if (entry != null && entry.getValue() != null && entry.getKey() != null) {
					xmlSerializer.startTag("", entry.getKey());
					xmlSerializer.text(entry.getValue());
					xmlSerializer.endTag("", entry.getKey());
				}
			}
			xmlSerializer.endTag("", "EPOSPROTOCOL");
			xmlSerializer.endDocument();
			String str = xmlWriter.toString();
			str = str.replace("encoding='UTF-8' ?", "encoding=\"UTF-8\"?");
			str = str.replace("version='1.0'", "version=\"1.0\"");
			return str;
		} catch (Exception e) {
//			KLog.e(TAG,  "Exception = " + e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

}
